/***********************************************************************************
Copy right:	    hqyj Tech.
Author:         jiaoyue
Date:           2023.07.01
Description:    http请求处理
***********************************************************************************/
#include <modbus-tcp.h>
#include <sys/msg.h>
#include "custom_handle.h"
#include "cJSON.h"
#include "msg_queue_peer.h"
#include "shmem.h"

// 定义 KB 为 1024，用于方便表示内存大小等单位
#define KB 1024
// 定义 HTML_SIZE 为 64KB，用于存储 HTML 内容的缓冲区大小
#define HTML_SIZE (64 * KB)

// 普通的文本回复需要增加html头部
#define HTML_HEAD "Content-Type: text/html\r\n" \
                  "Connection: close\r\n"
key_t key;

extern int shmid;
extern int *total;
extern struct std_node *node_arr;

// 　读取共享内存
static int handle_get(int sock, const char *input)
{
    printf("cccccccccccccccccccccccccccctotal:%d\n", *total);
    // 根据不同的请求进行处理
    if (strcmp(input, "getTempHum") != 0)
    {
        // 处理温湿度请求
        printf("!!!!!!!!!!!!!!!!!!!!input:%s\n", input);
        // 遍历结构体数组，找到温湿度对应的key值数据
        char temp_buf[128] = {0};
        char hum_buf[128] = {0};
        printf("total:%d\n", *total);
        for (int i = 0; i < *total; i++)
        {
            printf("key:%d\n", node_arr[i].key);
            if (node_arr[i].key == 203)
            {
                float temp_val = node_arr[i].new_val.f_val;
                sprintf(temp_buf, "temp:%.2f", temp_val);
            }
            if (node_arr[i].key == 204)
            {
                float hum_val = node_arr[i].new_val.f_val;
                sprintf(hum_buf, "hum:%.2f", hum_val);
            }
        }

        // 拼接温度和湿度消息
        char reply_buf[256] = {0};
        sprintf(reply_buf, "%s;%s", temp_buf, hum_buf);
        printf("dasdasdasdasdasdsa:buf:%s\n", reply_buf);
        send(sock, reply_buf, strlen(reply_buf), 0);
    }
    // 完成数据读取后，可根据实际情况决定是否分离共享内存地址
    // shmdt(node_p);
    else if (strcmp(input, "getLevel") == 0)
    {
        if (node_arr[2].key == 205)
        {
            float level_val = node_arr[2].new_val.f_val;
            char reply_buf[128] = {0};
            sprintf(reply_buf, "level:%.2f", level_val);
            printf("dasdasdasdasdasdsa:buf:%s\n", reply_buf);
            send(sock, reply_buf, strlen(reply_buf), 0);
        }

        // 遍历结构体数组，找到电量对应的key值数据
        // for (int i = 0; i < *total; i++)
        // {
        //     if (node_arr[i].key == 205)
        //     {
        //         float level_val = node_arr[i].new_val.f_val;
        //         char reply_buf[128] = {0};
        //         sprintf(reply_buf, "level:%.2f", level_val);
        //         printf("dasdasdasdasdasdsa:buf:%s\n", reply_buf);
        //         send(sock, reply_buf, strlen(reply_buf), 0);
        //         break;
        //     }
        // }
        // 完成数据读取后，可根据实际情况决定是否分离共享内存地址
        // shmdt(node_p);
    }
    else
    {
        send(sock, "Invalid request", strlen("Invalid request"), 0);
        return -1;
    }
    return 0;
}

// 消息队列下发
static int handle_set(int sock, const char *input)
{
    struct msgbuf send_buf;
    cJSON *root = cJSON_Parse(input);
    if (root == NULL)
    {
        printf("root is err\n");
        return -1;
    }
    cJSON *dev = cJSON_GetObjectItem(root, "dev");
    if (dev == NULL)
    {
        printf("root_dev iss err\n");
        return -1;
    }
    // 如果是单片机的设备
    if (strcmp(dev->valuestring, "stm32") == 0)
    {
        send_buf.mtype = 101;
        cJSON *name = cJSON_GetObjectItem(root, "name");
        // 如果是风扇
        if (strcmp(name->valuestring, "fan") == 0)
        {
            cJSON *val = cJSON_GetObjectItem(root, "val");
            switch (val->valueint)
            {
            case 0:
                sprintf(send_buf.mdata, "key:201,val:0");
                break;
            case 1:
                sprintf(send_buf.mdata, "key:201,val:1");
                break;
            case 2:
                sprintf(send_buf.mdata, "key:201,val:2");
                break;
            case 3:
                sprintf(send_buf.mdata, "key:201,val:3");
                break;
            }
            printf("fan: %s\n", send_buf.mdata);
            if (msg_queue_send("stm32", &send_buf, sizeof(send_buf), 0) < 0)
            {
                printf("msg_queue_send error1\n");
                return -1;
            }
        }
        // 如果是蜂鸣器
        else if (strcmp(name->valuestring, "buzzer") == 0)
        {
            cJSON *val = cJSON_GetObjectItem(root, "val");
            sprintf(send_buf.mdata, "key:202,val:%d", val->valueint);
            if (msg_queue_send("stm32", &send_buf, sizeof(send_buf), 0) < 0)
            {
                printf("msg_queue_sned error1\n");
                return -1;
            }
        }
    }
    // 如果是modbus
    else if (strcmp(dev->valuestring, "modbus") == 0)
    {
        send_buf.mtype = 102;
        cJSON *name = cJSON_GetObjectItem(root, "name");
        // 如果是车门
        if (strcmp(name->valuestring, "door") == 0)
        {
            cJSON *val = cJSON_GetObjectItem(root, "val");
            sprintf(send_buf.mdata, "key:103,val:%d", val->valueint);
            printf("door:%s\n", send_buf.mdata);
            if (msg_queue_send("modbus", &send_buf, sizeof(send_buf), 0) < 0)
            {
                printf("msg_queue_send error1\n");
                return -1;
            }
        }
        // 如果是雨刷
        else if (strcmp(name->valuestring, "wiper") == 0)
        {
            cJSON *val = cJSON_GetObjectItem(root, "val");
            sprintf(send_buf.mdata, "key:102,val:%d", val->valueint);
            printf("wiper:%s\n", send_buf.mdata);
            if (msg_queue_send("modbus", &send_buf, sizeof(send_buf), 0) < 0)
            {
                printf("msg_queue_sned error\n");
                return -1;
            }
        }
        // 如果是天窗
        else if (strcmp(name->valuestring, "roof") == 0)
        {
            cJSON *val = cJSON_GetObjectItem(root, "val");
            sprintf(send_buf.mdata, "key:104,val:%d", val->valueint);
            printf("roof:%s\n", send_buf.mdata);
            if (msg_queue_send("modbus", &send_buf, sizeof(send_buf), 0) < 0)
            {
                printf("msg_queue_send error\n");
                return -1;
            }
        }
        // 　如果是灯光
        else if (strcmp(name->valuestring, "light") == 0)
        {
            cJSON *val = cJSON_GetObjectItem(root, "val");
            if (val->valueint == 0)
            {
                sprintf(send_buf.mdata, "key:101,val:0");
            }
            else if (val->valueint == 1)
            {
                sprintf(send_buf.mdata, "key:101,val:1");
            }
            else if (val->valueint == 2)
            {
                sprintf(send_buf.mdata, "key:101,val:2");
            }
            printf("light: %s\n", send_buf.mdata);
            if (msg_queue_send("modbus", &send_buf, sizeof(send_buf), 0) < 0)
            {
                printf("msg_queue_sned error\n");
                return -1;
            }
        }
    }
}

/**
 * @brief 处理自定义请求，在这里添加进程通信
 * @param input
 * @return
 */
int parse_and_process(int sock, const char *query_string, const char *input)
{
    // 处理get
    if (strstr(input, "getTempHum"))
    {
        return handle_get(sock, input);
    }
    else if (strstr(input, "getLevel"))
    {
        return handle_get(sock, input);
    }
    // 处理set
    // 处理车门
    else if (strstr(input, "{\"dev\":\"modbus\",\"name\":\"door\",\"val\":1}"))
    {
        return handle_set(sock, input);
    }
    else if (strstr(input, "{\"dev\":\"modbus\",\"name\":\"door\",\"val\":0}"))
    {
        return handle_set(sock, input);
    }
    // 处理雨刷器
    else if (strstr(input, "{\"dev\":\"modbus\",\"name\":\"wiper\",\"val\":1}"))
    {
        return handle_set(sock, input);
    }
    else if (strstr(input, "{\"dev\":\"modbus\",\"name\":\"wiper\",\"val\":0}"))
    {
        return handle_set(sock, input);
    }
    // 处理天窗
    else if (strstr(input, "{\"dev\":\"modbus\",\"name\":\"roof\",\"val\":1}"))
    {
        return handle_set(sock, input);
    }
    else if (strstr(input, "{\"dev\":\"modbus\",\"name\":\"roof\",\"val\":0}"))
    {
        return handle_set(sock, input);
    }
    // 处理远近光灯
    else if (strstr(input, "{\"dev\":\"modbus\",\"name\":\"light\",\"val\":0}"))
    {
        return handle_set(sock, input);
    }
    else if (strstr(input, "{\"dev\":\"modbus\",\"name\":\"light\",\"val\":1}"))
    {
        return handle_set(sock, input);
    }
    else if (strstr(input, "{\"dev\":\"modbus\",\"name\":\"light\",\"val\":2}"))
    {
        return handle_set(sock, input);
    }
    // 　处理寻车
    else if (strstr(input, "{\"dev\":\"stm32\",\"name\":\"buzzer\",\"val\":1}"))
    {
        return handle_set(sock, input);
    }
    // 　处理寻车
    else if (strstr(input, "{\"dev\":\"stm32\",\"name\":\"buzzer\",\"val\":1}"))
    {
        return handle_set(sock, input);
    }
    else if (strstr(input, "{\"dev\":\"stm32\",\"name\":\"buzzer\",\"val\":0}"))
    {
        return handle_set(sock, input);
    }
    // 　处理风速
    else if (strstr(input, "{\"dev\":\"stm32\",\"name\":\"fan\",\"val\":0}"))
    {
        return handle_set(sock, input);
    }
    else if (strstr(input, "{\"dev\":\"stm32\",\"name\":\"fan\",\"val\":1}"))
    {
        return handle_set(sock, input);
    }
    else if (strstr(input, "{\"dev\":\"stm32\",\"name\":\"fan\",\"val\":2}"))
    {
        return handle_set(sock, input);
    }
    else if (strstr(input, "{\"dev\":\"stm32\",\"name\":\"fan\",\"val\":3}"))
    {
        return handle_set(sock, input);
    }
    else // 剩下的都是json请求，这个和协议有关了
    {
        // 构建要回复的JSON数据
        const char *json_response = "{\"message\": \"Hello, client!\"}";
        // 发送HTTP响应给客户端
        send(sock, json_response, strlen(json_response), 0);
    }
    return 0;
}